package cn.tedu.luban.worker.main.converter;

import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import cn.tedu.luban.worker.protocol.bo.WorkerCategoryBO;
import cn.tedu.luban.worker.protocol.dos.WorkerCategoryDO;
import cn.tedu.luban.worker.protocol.param.WorkerRegistCategoryParam;
import com.alibaba.nacos.common.utils.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;


@Component
public class WorkerCategoryConverter {
    public List<WorkerCategoryDO> params2dos(List<WorkerRegistCategoryParam> params,Long userId){
        List<WorkerCategoryDO> workerCategoryDOS = new ArrayList<>(params.size());
        if (CollectionUtils.isNotEmpty(params)){
            for (WorkerRegistCategoryParam param : params) {
                WorkerCategoryDO workerCategoryDO = this.param2do(param);
                workerCategoryDO.setUserId(userId);
                workerCategoryDOS.add(workerCategoryDO);
            }
        }
        return workerCategoryDOS;
    }
    public WorkerCategoryDO param2do(WorkerRegistCategoryParam param) {
        WorkerCategoryDO workerCategory = new WorkerCategoryDO();
        BeanUtils.copyProperties(param, workerCategory);
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        workerCategory.setGmtCreate(System.currentTimeMillis());
        workerCategory.setGmtModified(workerCategory.getGmtCreate());
        workerCategory.setCreateUserId(loginUser != null ? loginUser.getId() : 0);
        workerCategory.setModifiedUserId(loginUser != null ? loginUser.getId() : 0);
        workerCategory.setCreateUserName(loginUser != null ? loginUser.getUsername() : "mock");
        workerCategory.setModifiedUserName(loginUser != null ? loginUser.getUsername() : "mock");
        //默认是开启
        workerCategory.setStatus(1);
        return workerCategory;
    }

    public List<WorkerCategoryBO> do2bos(List<WorkerCategoryDO> dos) {
        List<WorkerCategoryBO> workerCategoryBOS = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dos)){
            for (WorkerCategoryDO workerCategoryDO : dos) {
                workerCategoryBOS.add(this.do2bo(workerCategoryDO));
            }
        }
        return workerCategoryBOS;
    }
    public List<WorkerCategoryBO> dos2bos(List<WorkerCategoryDO> dos) {
        List<WorkerCategoryBO> workerCategoryBOS = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dos)){
            for (WorkerCategoryDO workerCategoryDO : dos) {
                workerCategoryBOS.add(this.do2bo(workerCategoryDO));
            }
        }
        return workerCategoryBOS;
    }
    private WorkerCategoryBO do2bo(WorkerCategoryDO workerCategoryDO) {
        WorkerCategoryBO workerCategoryBO = new WorkerCategoryBO();
        if (workerCategoryDO != null) {
            BeanUtils.copyProperties(workerCategoryDO, workerCategoryBO);
        }
        return workerCategoryBO;
    }
}